package gmu.swe.project.registry;

import gmu.swe.project.tracker.TrackerInfo;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class RegistryServiceThread extends Thread {

	private Socket socket = null;

	public RegistryServiceThread(Socket socket) {

		super("RegistryServiceThread");
		this.socket = socket;
	}

	public void run() {

		try {
			System.out.println("RegistryServiceThread::Registry service thread spawned");

			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
			
			Object obj = in.readObject();
			
			//if object is an instance of TrackerInfo, update the tracker registration
			//else it's a request coming from the registry requesting for the latest tracker
			//registration, which is sent back regardless of the type of request that comes in
			if (obj instanceof TrackerInfo) {
				RegistryServer.updateTrackerRegistration((TrackerInfo) obj);
			}
			
			out.writeObject(RegistryServer.getTrackerRegistration());
			out.close();
			in.close();
			socket.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
